/*
 * MyTableModel.java
 *
 * Copyright 2011 Universidad Francisco de Paula Santander (UFPS).
 * Todos los Derechos Reservados.
 * Creado el 10/03/2011, 11:23:38 AM
 */

package com.ufps.cuatro.util.model;

import java.io.Serializable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

/**
 * <i><b>MyTableModel:</b></i> @DefinicionClase
 * @author Carlos A. Torres C. - Dolly E. Guerrero. <https://code.google.com/p/tesis4dor/>
 * @version 1.0 10/03/2011
 * @see AbstractTableModel
 * @see Serializable
 * @see TableModel
 */
public class MyTableModel extends AbstractTableModel implements Serializable, TableModel {
    private static final long serialVersionUID = 875227410783782538L;
    protected String[] columnNames;
    protected Object[][] data;
    /**
     * @DefinicionAtributo
     */
    public final Object[] longValues = null;
    protected boolean DEBUG = false;
    protected boolean[][] indicesNoEditables;    

    /**
     * @DefinicionMetodo
     * @param columnNames @DefinicionParametro
     * @param data @DefinicionParametro
     */
    public MyTableModel(String[] columnNames, Object[][] data) {
        this.columnNames = columnNames;
        this.data = data;    
    }

    /**
     * @DefinicionMetodo
     * @param columnNames @DefinicionParametro
     * @param data @DefinicionParametro
     * @param indicesNoEditables @DefinicionParametro
     */
    public MyTableModel(String[] columnNames, Object[][] data, boolean[][] indicesNoEditables) {
        this.columnNames = columnNames;
        this.data = data;
        this.indicesNoEditables = indicesNoEditables;
    }

    public int getColumnCount() {
       try{
            return columnNames.length;
        }catch(java.lang.NullPointerException ex){}
       return 0;
    }

    public int getRowCount() {
       try{
        return data.length;
        }catch(java.lang.NullPointerException ex){}
       return 0;
    }

    @Override
    public String getColumnName(int col) {
       try{
        return columnNames[col];
        }catch(java.lang.NullPointerException ex){}
       return "";
    }

    public Object getValueAt(int row, int col) {
       try{
        return data[row][col];
        }catch(java.lang.NullPointerException ex){}
       return null;
    }

    /*
     * JTable uses this method to determine the default renderer/
     * editor for each cell.  If we didn't implement this method,
     * then the last column would contain text ("true"/"false"),
     * rather than a check box.
     */
    @Override
    public Class getColumnClass(int c) {
        try{
            return getValueAt(0, c).getClass();
        }catch(java.lang.Exception ex){
            return super.getColumnClass(c);
        }
    }

    /*
     * Don't need to implement this method unless your table's
     * editable.
     */
    @Override
    public boolean isCellEditable(int row, int col) {
        //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.
        if(this.indicesNoEditables != null){
            return this.indicesNoEditables[row][col];
        }
        return true;
    }

    /*
     * Don't need to implement this method unless your table's
     * data can change.
     */
    @Override
    public void setValueAt(Object value, int row, int col) {
       try{ 
            if (DEBUG) {
                System.out.println("Setting value at " + row + "," + col
                                   + " to " + value
                                   + " (an instance of "
                                   + value.getClass() + ")");
            }

            //JOptionPane.showMessageDialog(CuatroApp.getApplication().getMainFrame(), "Actual: " + data[row][col] + " Se envio: " + value + " Fila: " + row + " Columna: " + col);

            data[row][col] = value;
            fireTableCellUpdated(row, col);

            if (DEBUG) {
                System.out.println("New value of data:");
                printDebugData();
            }
        }catch(java.lang.NullPointerException ex){}
    }

    private void printDebugData() {
        int numRows = getRowCount();
        int numCols = getColumnCount();

        for (int i=0; i < numRows; i++) {
            System.out.print("    row " + i + ":");
            for (int j=0; j < numCols; j++) {
                System.out.print("  " + data[i][j]);
            }
            System.out.println();
        }
        System.out.println("--------------------------");
    }   
    
    



}
